<template>
  <el-dialog
    :title="!dataForm.brandId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px" v-loading="dataFormLoading">
      <el-form-item label="品牌名" prop="name"  style="text-align: left">
        <el-input v-model="dataForm.name" placeholder="品牌名"></el-input>
      </el-form-item>
      <el-form-item label="品牌logo地址" prop="logo">
        <SingleUpload v-model="dataForm.logo"></SingleUpload>
      </el-form-item>
      <el-form-item label="介绍" prop="descript">
        <el-input v-model="dataForm.descript" placeholder="介绍"></el-input>
      </el-form-item>
      <el-form-item label="显示状态" prop="showStatus" v-if="!dataForm.brandId">
        <el-switch
          v-model="dataForm.showStatus"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0">
        </el-switch>
      </el-form-item>
      <el-form-item label="检索首字母" prop="firstLetter">
        <el-input v-model="dataForm.firstLetter" placeholder="检索首字母"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model.number="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import { info, add, update } from '../../../api/goods/brand'
import SingleUpload from '@/components/Upload/SingleUpload'

export default {
  components: {
    SingleUpload
  },
  data() {
    return {
      visible: false,
      dataFormLoading: false,
      dataForm: {
        brandId: 0,
        name: '',
        logo: '',
        descript: '',
        showStatus: 1,
        firstLetter: '',
        sort: 0
      },
      dataRule: {
        name: [
          { validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('品牌名不能为空'))
              } else if (value.length > 10) {
                callback(new Error('品牌名不能超过10个字'))
              } else {
                callback()
              }
            }, trigger: 'blur' }
        ],
        logo: [
          { required: true, message: '品牌logo地址不能为空', trigger: 'blur' }
        ],
        descript: [
          { validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('介绍不能为空'))
              } else if (value.length > 20) {
                callback(new Error('介绍不能超过20个字'))
              } else {
                callback()
              }
            }, trigger: 'blur' }
        ],
        firstLetter: [
          { validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('首字母必须填写'))
              } else if ( !/^[a-zA-Z]$/.test(value)) {
                callback(new Error('首字符必须 a-z 或 A-Z 之间'))
              } else {
                callback()
              }
            }, trigger: 'blur' }
        ],
        sort: [
          { validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('排序必须填写'))
              } else if ( !Number.isInteger(value) || value < 0 ) {
                callback(new Error('排序必须是一个大于等于零的整数'))
              } else {
                callback()
              }
            }, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.brandId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.brandId) {
          this.dataFormLoading = true
          info(this.dataForm.brandId)
            .then(({ data }) => {
              this.dataForm.name = data.name
              this.dataForm.logo = data.logo
              this.dataForm.descript = data.descript
              this.dataForm.showStatus = data.showStatus
              this.dataForm.firstLetter = data.firstLetter
              this.dataForm.sort = data.sort
              this.dataFormLoading = false
            })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const brand = {
            'brandId': this.dataForm.brandId || undefined,
            'name': this.dataForm.name,
            'logo': this.dataForm.logo,
            'descript': this.dataForm.descript,
            'showStatus': this.dataForm.showStatus,
            'firstLetter': this.dataForm.firstLetter,
            'sort': this.dataForm.sort
          }
          if(!this.dataForm.brandId){
            add(brand)
              .then(() => {
                this.$message({
                  message: '添加成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('refreshDataList')
              })
          } else {
            update(brand)
              .then(() => {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('refreshDataList')
              })
          }
        }
      })
    }
  }
}
</script>
